<script lang="ts" setup>
import type { SopIsvInfoModel } from '#/api/devOperation/sop/model/sopIsvInfoModel';

import { onMounted, ref } from 'vue';

import { useFs } from '@fast-crud/fast-crud';
import { Button } from 'ant-design-vue';

import { getValidateRulesByFs } from '#/api/common/validateByFs';
import {
  SopIsvInfoApi,
  SopIsvInfoConfig,
} from '#/api/devOperation/sop/sopIsvInfo';
import { ActionEnum } from '#/enums/commonEnum';
import { useMessage } from '#/hooks/web/useMessage';

import { createCrudOptions, frontRules } from './data/crud';
import ShowKeys from './show-keys.vue';

defineOptions({
  name: '开放应用',
  inheritAttrs: false,
});

const { createMessage } = useMessage();
const showKeysRef = ref();
const { crudRef, crudBinding, crudExpose, appendCrudOptions } = useFs({
  createCrudOptions,
  context: { showKeysRef },
});

// 页面打开后获取列表数据
onMounted(async () => {
  const addFormOptions = await getValidateRulesByFs({
    Api: SopIsvInfoConfig.ApplyFor,
    mode: ActionEnum.ADD,
    customRules: frontRules(crudExpose, ActionEnum.ADD),
  });
  const editFormOptions = await getValidateRulesByFs({
    Api: SopIsvInfoConfig.UpdateOpen,
    mode: ActionEnum.EDIT,
    customRules: frontRules(crudExpose, ActionEnum.EDIT),
  });
  appendCrudOptions({ ...addFormOptions, ...editFormOptions });
  crudExpose.doRefresh();
});

async function handleSubmit() {
  await crudExpose.getFormRef()?.validate();
  const form = crudExpose.getFormData() as SopIsvInfoModel.SopIsvInfoSubmitVO;

  await SopIsvInfoApi.submit(form);
  createMessage.success('提交成功');
  crudExpose.getFormWrapperRef().close();
  crudExpose.doRefresh();
}
</script>

<template>
  <FsPage>
    <FsCrud ref="crudRef" v-bind="crudBinding">
      <template #form-footer-right="{ mode, getFormData }">
        <Button
          v-if="
            mode === ActionEnum.EDIT &&
            [0, 99].includes(getFormData()?.auditStatus)
          "
          type="primary"
          @click="handleSubmit"
        >
          提交审核
        </Button>
      </template>
    </FsCrud>
    <ShowKeys ref="showKeysRef" />
  </FsPage>
</template>
<style lang="scss" scoped></style>
